﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LL.Business;
using NanoERP.Model;

namespace NanoERP.Business.BaseInfo
{
    /// <summary>
    /// 收付款账户信息业务类
    /// </summary>
    public class FlowinfoBLL : ServiceBase
    {
        /// <summary>
        /// 单据审核循环设置查询
        /// </summary>
        /// <returns></returns>
        public List<Flowinfo> Query()
        {
            var list = this.DbSession.GetAll<Flowinfo>().ToList();

            return list;
        }

        /// <summary>
        /// 审核环节数查询
        /// </summary>
        /// <param name="flowId">单据审核循环设置ID</param>
        /// <returns></returns>
        public List<Flowphase> QueryFlowphase(long flowId)
        {
            return DbSession.Query<Flowphase>("FLOWID=@0", flowId).ToList();
        }

        /// <summary>
        /// 单据审核循环设置
        /// </summary>
        /// <param name="id">单据审核循环设置ID</param>
        /// <returns></returns>
        public Flowinfo Get(long id)
        {
            var o = this.DbSession.Get<Flowinfo>(id);
            return o;
        }

        /// <summary>
        /// 编辑单据审核循环设置
        /// </summary>
        /// <param name="o">单据审核循环设置对象</param>
        /// <param name="list">审核环节数集合</param>
        public void Edit(Flowinfo o, List<Flowphase> list)
        {
            if (o.Id == 0)
                this.DbSession.Insert(o, "Id");
            else
                this.DbSession.Update(o);

            list.ForEach(r =>
            {
                var phase = DbSession.Query<Flowphase>("FLOWID=@0 AND PHASENAME=@1", r.Flowid, r.Phasename).First();
                if (phase == null)
                {
                    this.DbSession.Insert(r, "Id");
                    return;
                }
                else
                {
                    phase.Phaseuser = r.Phaseuser;
                    phase.Isfinish = r.Isfinish;
                }
                this.DbSession.Update(phase);
            });
        }

        /// <summary>
        /// 删除单据审核循环设置
        /// </summary>
        /// <param name="id"></param>
        public void Delete(long id)
        {
            this.DbSession.Delete<Flowinfo>(id);
        }
    }
}
